package com.basic.modules.ykf.controller;

import java.util.Arrays;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.validator.ValidatorUtils;

import com.basic.modules.ykf.service.YkfDictDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.basic.modules.ykf.entity.YkfCustomGroupEntity;
import com.basic.modules.ykf.service.YkfCustomGroupService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;



/**
 * 客服分组管理
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-10-16 17:18:37
 */
@RestController
@RequestMapping("ykf/customGroup")
public class YkfCustomGroupController {

    @Autowired
    private YkfCustomGroupService ykfCustomGroupService;

    @Autowired
    private YkfDictDetailService ykfDictDetailService;

    /**
     * 列表-分页
     */
    @PostMapping("/page")
    public R list(@RequestBody ParamsModel<YkfCustomGroupEntity> params){
        PageUtils page = ykfCustomGroupService.queryPage(params);
        return R.ok(page);
    }

    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfCustomGroupEntity param){
        if (ObjectUtil.isEmpty(param) || ObjectUtil.isEmpty(param.getCustomGroupId())) {
            throw new RRException("参数缺失");
        }
        return R.ok(ykfCustomGroupService.getById(param.getCustomGroupId()));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfCustomGroupEntity ykfCustomGroup){
        ValidatorUtils.validateEntity(ykfCustomGroup);
		ykfCustomGroupService.save(ykfCustomGroup);
        ykfDictDetailService.updateDictMap("customGroup", StrUtil.toString(ykfCustomGroup.getCustomGroupId()), ykfCustomGroup.getCustomGroupName());
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfCustomGroupEntity ykfCustomGroup){
        ValidatorUtils.validateEntity(ykfCustomGroup);
		ykfCustomGroupService.updateById(ykfCustomGroup);
        ykfDictDetailService.updateDictMap("customGroup", StrUtil.toString(ykfCustomGroup.getCustomGroupId()), ykfCustomGroup.getCustomGroupName());
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] customGroupIds){
		ykfCustomGroupService.removeByIds(Arrays.asList(customGroupIds));
        for (Long customGroupId : customGroupIds) {
            ykfDictDetailService.updateDictMap("customGroup", StrUtil.toString(customGroupId), "");
        }
        return R.ok();
    }

}
